﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmDanhmucLopHoc : Form
    {
        int index;      //ví trí của sinh viên đó trong dữ liệu
        int count;      //Số lượng sinh viên
        bool xoa;
        bool temp;       //tùy chọn insert or update
        string manganh;
        SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SM;Integrated Security=True");
        string sql = "Select * from T03_Lop_hoc lh, T05_Nganh_hoc nh where lh.ManganhID = nh.ManganhID";
        public frmDanhmucLopHoc()
        {
            InitializeComponent();
            string get_lkuManganh = "Select * from T05_Nganh_hoc";
            DataSet ds1 = FillDataset(get_lkuManganh);
            lkuManganh.Properties.DataSource = ds1.Tables[0];
            lkuManganh.Properties.DisplayMember = "Tennganh";
            lkuManganh.Properties.ValueMember = "ManganhID";
            disable_textbox();
        }

        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
            return ds;
        }

        public void query(string command)
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
            connection.Open();
            SqlCommand sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = command;
            SqlDataReader dr = sqlCommand.ExecuteReader();
            connection.Close();
        }

        public bool getRowsTable(string str)
        {
            connection.Open();
            SqlCommand sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = str;
            SqlDataReader dr = sqlCommand.ExecuteReader();
            if (dr.HasRows == true)
            {
                connection.Close();
                return true;
            }
            connection.Close();
            return false;
        }

        public void reset_lookupedit()
        {
            lkuManganh.Refresh();
        }

        public void get_gridviewdanhsachsv()
        {
            DataSet ds = FillDataset("select MasvID, Holot, Ten, Noisinh, Ngaysinh, Diachi, lh.Namvaohoc, dt.Tendantoc, tg.Tentongiao from T01_Sinh_vien sv, T03_Lop_hoc lh, T05_Nganh_hoc nh, T07_Dan_toc dt, T08_Ton_giao tg where sv.MalopID = lh.MalopID and lh.ManganhID = nh.ManganhID and tg.MatongiaoID = sv.MatongiaoID and dt.MadantocID = sv.MadantocID and lh.MalopID = N'" + txtMalop.Text + "'");
            gCol_MaSV.FieldName = "MasvID";
            gCol_Holot.FieldName = "Holot";
            gCol_Ten.FieldName = "Ten";
            gCol_Noisinh.FieldName = "Noisinh";
            gCol_Ngaysinh.FieldName = "Ngaysinh";
            gCol_Diachi.FieldName = "Diachi";
            gCol_Dantoc.FieldName = "Tendantoc";
            gCol_Tongiao.FieldName = "Tentongiao";
            gridControl1.DataSource = ds.Tables[0];
        }

        public void get_Tabdanhsachlophoc(string sql, DataRow row)
        {
            reset_lookupedit();
            txtMalop.Text = row["MalopID"].ToString();
            txtTenlop.Text = row["Tenlop"].ToString();
            txtNamvaohoc.Text = row["Namvaohoc"].ToString();
            lkuManganh.Text = row["Tennganh"].ToString();
            lkuManganh.ClosePopup();
            mGhichu.Text = row["Ghichu"].ToString();
            manganh = row["ManganhID"].ToString();
        }

        public void get_lay_Makhoa()
        {
            DataSet ds = FillDataset("Select MalopID from T03_Lop_hoc");
            lay_colMakhoa.FieldName = "MalopID";
            gridControl2.DataSource = ds.Tables[0];
        }

        private void frmDanhmucLopHoc_Load(object sender, EventArgs e)
        {
            get_lay_Makhoa();
            DataSet ds = FillDataset(sql);
            DataRow row = ds.Tables[0].Rows[0];
            count = ds.Tables[0].Rows.Count;
            get_Tabdanhsachlophoc(sql, row);
            get_gridviewdanhsachsv();

        }

        private void layoutView1_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            int[] i = layoutView1.GetSelectedRows();
            int j = i[0];
            DataSet ds = FillDataset("select MalopID from T03_Lop_hoc");
            string a = ds.Tables[0].Rows[j].ItemArray[0].ToString();
            DataSet ds1 = FillDataset("Select * from T03_Lop_hoc lh, T05_Nganh_hoc nh where lh.ManganhID = nh.ManganhID and MalopID = N'" + a + "'");
            DataRow row = ds1.Tables[0].Rows[0];
            index = j;
            get_Tabdanhsachlophoc(sql, row);
            get_gridviewdanhsachsv();

        }

        private void btnVedau_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            DataSet ds = FillDataset(sql);
            DataRow row = ds.Tables[0].Rows[0];
            get_Tabdanhsachlophoc(sql, row);
            index = ds.Tables[0].Rows.IndexOf(row);
            get_gridviewdanhsachsv();
        }

        private void btnLui_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            index--;
            if (index >= 0)
            {
                DataSet ds = FillDataset(sql);
                DataRow row = ds.Tables[0].Rows[index];
                get_Tabdanhsachlophoc(sql, row);
                index = ds.Tables[0].Rows.IndexOf(row);
                get_gridviewdanhsachsv();
            }
            else
            {
                index++;
                if (xoa == false)
                    MessageBox.Show("Bạn đang ở mẫu tin đầu tiên");
                else
                    btnVedau_Click(sender, e);
            }
        }

        private void bntToi_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            index++;
            if (index < count)
            {
                DataSet ds = FillDataset(sql);
                DataRow row = ds.Tables[0].Rows[index];
                get_Tabdanhsachlophoc(sql, row);
                index = ds.Tables[0].Rows.IndexOf(row);
                get_gridviewdanhsachsv();
            }
            else
            {
                index--;
                MessageBox.Show("Bạn đang ở mẫu tin cuối cùng");
            }
        }

        private void btnVecuoi_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            DataSet ds = FillDataset(sql);
            DataRow row = ds.Tables[0].Rows[count - 1];
            get_Tabdanhsachlophoc(sql, row);
            index = ds.Tables[0].Rows.IndexOf(row);
            get_gridviewdanhsachsv();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            btnVedau.Enabled = false;
            btnVecuoi.Enabled = false;
            btnLui.Enabled = false;
            btnToi.Enabled = false;
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnThoat.Enabled = false;
            btnLuu.Enabled = true;
            btnBoqua.Enabled = true;
            txtMalop.Enabled = false;
            txtMalop.Text = "";
            txtTenlop.Text = "";
            txtNamvaohoc.Text = "";
            lkuManganh.EditValue = "";
            mGhichu.Text = "";
            enable_textbox();

        }

        public void disable_textbox()
        {
            txtMalop.Enabled = false;
            txtNamvaohoc.Enabled = false;
            txtTenlop.Enabled = false;
            lkuManganh.Enabled = false;
            mGhichu.Enabled = false;
        }

        public void enable_textbox()
        {
            txtMalop.Enabled = true;
            txtNamvaohoc.Enabled = true;
            txtTenlop.Enabled = true;
            lkuManganh.Enabled = true;
            mGhichu.Enabled = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBoqua_Click(object sender, EventArgs e)
        {
            btnVedau.Enabled = true;
            btnVecuoi.Enabled = true;
            btnLui.Enabled = true;
            btnToi.Enabled = true;
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnThoat.Enabled = true;
            btnLuu.Enabled = false;
            btnBoqua.Enabled = false;
            reset_lookupedit();
            DataSet ds = FillDataset(sql);
            DataRow row = ds.Tables[0].Rows[index];
            get_Tabdanhsachlophoc(sql, row);
            disable_textbox();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        { 
            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                if (temp == false)
                {
                    bool check = getRowsTable("select * from T03_Lop_hoc where MalopID = N'" + txtMalop.Text + "' ");
                    if (!check)
                    {
                        string str = "insert into T03_Lop_hoc (MalopID, Tenlop, Namvaohoc, ManganhID, Ghichu) values (N'" + txtMalop.Text + "', N'" + txtTenlop.Text + "', N'" + txtNamvaohoc.Text + "', N'" + manganh + "', N'" + mGhichu.Text + "')";
                        query(str);
                    }
                    else
                        MessageBox.Show("Mã lớp " + txtMalop.Text + " đã tồn tại. Xin vui lòng chọn mã lớp khác", "Thông báo");
                }
                else
                {
                    reset_lookupedit();
                    string str = "update T03_Lop_hoc set Tenlop = N'" + txtTenlop.Text + "', Namvaohoc = N'" + txtNamvaohoc.Text + "', ManganhID = N'" + manganh + "', Ghichu = N'" + mGhichu.Text + "' where MalopID = N'" + txtMalop.Text + "'";
                    query(str);
                }
                disable_textbox();
                btnVedau.Enabled = true;
                btnVecuoi.Enabled = true;
                btnLui.Enabled = true;
                btnToi.Enabled = true;
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnThoat.Enabled = true;
                btnLuu.Enabled = false;
                btnBoqua.Enabled = false;
                get_lay_Makhoa();
            }
            else
                MessageBox.Show("Bạn chưa nhập dữ liệu cần thiết", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.None);
       
        }

        

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa không?.", "Quản lý sinh viên", MessageBoxButtons.OKCancel, MessageBoxIcon.None) == DialogResult.OK)
            {
                
                bool check = getRowsTable("select * from T01_Sinh_Vien where MalopID = N'" + txtMalop.Text + "' ");
                if (check == false)
                {
                    xoa = true;
                    query("delete from T03_Lop_hoc where MalopID =N'" + txtMalop.Text + "'");
                    get_lay_Makhoa();
                    btnLui_Click(sender, e);
                    get_gridviewdanhsachsv();
                }
                else
                {
                    //"delete from T01_Sinh_Vien where MalophocID =N'" + txtMalop.Text + "' ";
                    MessageBox.Show("Lớp " + txtMalop.Text + "dã có sinh viên, nên không thể xóa được", "Thông báo", MessageBoxButtons.OK);
                }
                
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            temp = true;
            btnVedau.Enabled = false;
            btnVecuoi.Enabled = false;
            btnLui.Enabled = false;
            btnToi.Enabled = false;
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnThoat.Enabled = false;
            btnLuu.Enabled = true;
            btnBoqua.Enabled = true;
            txtNamvaohoc.Enabled = true;
            txtTenlop.Enabled = true;
            lkuManganh.Enabled = true;
            mGhichu.Enabled = true;
        }

        private void lkuManganh_EditValueChanged(object sender, EventArgs e)
        {
            manganh = lkuManganh.EditValue.ToString();
        }
    }
}
